WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
factory.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// wolf
22#include "core/common/wolf.h"
23#include "core/utils/unused.h"
24
25// std
26#include <string>
27#include <map>
28#include <list>
29#include <iostream>
30#include <iomanip>
31
32namespace wolf
33{
236template <class TypeBase, typename... TypeInput>
238{
239 public:
241 typedef TypeBase (*CreatorCallback)(TypeInput... _input);
242
243 private:
244 typedef std::map<std::string, CreatorCallback> CallbackMap;
245
246 public:
249 static bool registerCreator(const std::string& _type, CreatorCallback createFn);
250
253 static bool unregisterCreator(const std::string& _type);
254
257 static bool isCreatorRegistered(const std::string& _type);
258
261 static TypeBase create(const std::string& _type, TypeInput... _input);
262
265 std::string getClass() const;
266
269 static void printAddress();
270
273 static void printCallbacks();
274
277 static std::list<std::string> getRegisteredKeys();
278
279 private:
280 CallbackMap callbacks_;
281
282 // Singleton ---------------------------------------------------
283 // This class is a singleton. The code below guarantees this.
284 // See: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
285 private:
288 static Factory& get();
289
290 public:
293 Factory(const Factory&) = delete;
294
297 void operator=(Factory const&) = delete;
298
299 private:
302 Factory();
303
306 ~Factory();
307};
308
309template <class TypeBase, typename... TypeInput>
311{
312 // std::cout << " Factory constructor " << this->getClass() << std::endl;
313}
314
315template <class TypeBase, typename... TypeInput>
316inline Factory<TypeBase, TypeInput...>::~Factory()
317{
318 // std::cout << " Factory destructor " << this->getClass() << std::endl;}
319}
320
321template <class TypeBase, typename... TypeInput>
322inline bool Factory<TypeBase, TypeInput...>::registerCreator(const std::string& _type, CreatorCallback createFn)
323{
324 if (get().callbacks_.count(_type) == 1 and get().callbacks_.at(_type) != createFn)
325 throw std::runtime_error(get().getClass() + " : For the type \"" + _type +
326 "\": Trying to register a creator but there is a different creator registered.");
327
328 bool reg = get().callbacks_.insert(typename CallbackMap::value_type(_type, createFn)).second;
329
330 if (reg) std::cout << std::setw(31) << std::left << get().getClass() << " <-- registered " << _type << std::endl;
331
332 return reg;
333}
334
335template <class TypeBase, typename... TypeInput>
336inline bool Factory<TypeBase, TypeInput...>::unregisterCreator(const std::string& _type)
337{
338 return get().callbacks_.erase(_type) == 1;
339}
340
341template <class TypeBase, typename... TypeInput>
342inline bool Factory<TypeBase, TypeInput...>::isCreatorRegistered(const std::string& _type)
343{
344 return get().callbacks_.count(_type) == 1;
345}
346
347template <class TypeBase, typename... TypeInput>
348inline TypeBase Factory<TypeBase, TypeInput...>::create(const std::string& _type, TypeInput... _input)
349{
350 typename CallbackMap::const_iterator creator_callback_it = get().callbacks_.find(_type);
351
352 if (creator_callback_it == get().callbacks_.end())
353 // not found
354 throw std::runtime_error(get().getClass() + " : Unknown type \"" + _type +
355 "\". Possibly you tried to use an unregistered creator.");
356
357 // Invoke the creation function
358 return (creator_callback_it->second)(std::forward<TypeInput>(_input)...);
359}
360
361template <class TypeBase, typename... TypeInput>
362inline Factory<TypeBase, TypeInput...>& Factory<TypeBase, TypeInput...>::get()
363{
364 static Factory<TypeBase, TypeInput...> instance_;
365 return instance_;
366}
367
368template <class TypeBase, typename... TypeInput>
370{
371 return "Factory<class TypeBase>";
372}
373
374template <class TypeBase, typename... TypeInput>
376{
377 std::cout << get().getClass() << " address: " << &get() << std::endl;
378}
379
380template <class TypeBase, typename... TypeInput>
382{
383 std::cout << get().getClass() << " callbacks size: " << get().callbacks_.size() << std::endl;
384 for (auto cb : get().callbacks_)
385 {
386 std::cout << "\t" << cb.first << std::endl;
387 }
388}
389
390template <class TypeBase, typename... TypeInput>
392{
393 std::list<std::string> keys;
394 for (auto cb : get().callbacks_)
395 {
396 keys.push_back(cb.first);
397 }
398 return keys;
399}
400
401} // namespace wolf
Singleton template factory.
Definition: factory.h:238
static void printCallbacks()
Prints the registered types.
Definition: factory.h:381
static bool registerCreator(const std::string &_type, CreatorCallback createFn)
Registers the creator of a type. Throws if a different creator is already registered for it.
Definition: factory.h:322
static TypeBase create(const std::string &_type, TypeInput... _input)
Creates an object of a type with its registered creator. Throws if no creator is registered.
Definition: factory.h:348
static std::list< std::string > getRegisteredKeys()
Returns the registered types.
Definition: factory.h:391
TypeBase(* CreatorCallback)(TypeInput... _input)
Example of creator callback (see typedefs below)
Definition: factory.h:241
static bool isCreatorRegistered(const std::string &_type)
Returns whether a creator is registered for a type.
Definition: factory.h:342
void operator=(Factory const &)=delete
Not assignable (singleton)
std::string getClass() const
Returns the name of the factory.
Definition: factory.h:369
Factory(const Factory &)=delete
Not copyable (singleton)
static void printAddress()
Prints the address of the factory singleton.
Definition: factory.h:375
static bool unregisterCreator(const std::string &_type)
Unregisters the creator of a type. Returns false if it was not registered.
Definition: factory.h:336
Definition: capture_base.h:23